﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jobs.Inf.Protector
{
    /// <summary>
    /// 应用程序级错误，出现这样的错误，通常是因为应用程序异常造成的
    /// </summary>
    public class CoreException : Exception
    {
        public CoreException(string message, ExceptionCode code)
            : base(message)
        {
            this.ExceptionCode = code;
        }
        public ExceptionCode ExceptionCode { get; set; }
    }
    /// <summary>
    /// 普通错误，出现这样的错误，通常是由于输入参数错误造成的
    /// </summary>
    public class ContextException : Exception
    {
        public ContextException(string message, ExceptionCode code)
            : base(message)
        {
            this.ExceptionCode = code;
        }
        public ExceptionCode ExceptionCode { get; set; }
    }
    /// <summary>
    /// 运行时异常，在程序执行过程中由于某些原因造成的异常
    /// </summary>
    public class RuntimeException : Exception
    {
        /// <summary>
        /// 格式化过后的错误信息
        /// </summary>
        public string ExceptionMessage { get; set; }
        /// <summary>
        /// 错误信息
        /// </summary>
        StringBuilder sb = new StringBuilder();
        public RuntimeException(string messageFormat, Exception rawException)
        {
            ExceptionVistor(rawException);
            ExceptionMessage = String.Format(messageFormat, sb.ToString());
            ExcuteException();
        }
        public RuntimeException(string message)
        {
            ExceptionMessage = message;
            ExcuteException();
        }
        private void ExceptionVistor(Exception e)
        {
            sb.Append(e.ToString());
            if (e.InnerException != null)
            {
                sb.Append("\r\n");
                ExceptionVistor(e.InnerException);
            }
        }

        protected virtual void ExcuteException()
        {
            
        }

    }
}
